﻿#pragma once

#include <string.h>

//typedef enum{
//	BMP_TYPE_8_GREY = 1,
//	BMP_TYPE_16_GREY,
//	BMP_TYPE_MAX
//}gtBmpType;

/*NOTE:用vs编译的时候必须设置“结构成员对齐为1字节，避免编译器对其优化”*/
#if defined(_WIN32)
#pragma pack(push)
#pragma pack(1)
struct gtBmpHeader{
    unsigned short identifiler;
    unsigned int file_size;
    unsigned short reversed1;  // 6,7  reused
    unsigned short reversed2;  // 8，9  reused
    unsigned int bitmap_data_offset;  // 10~13
    unsigned int bitmap_header_size;  // 14~17
    unsigned int width;  // 18~21
    unsigned int height; // 22~25
    unsigned short planes; // 26,27
    unsigned short bits_per_pixel;  //28,29
    unsigned int compression;  //30~33
    unsigned int bitmap_data_size; //34~37
    unsigned int hResolution; // 38~41  reused 26h
    unsigned int vResolution; //42~45   reused  2ah
    unsigned int colors; //46~49
    unsigned int important_colors; // 50~53  32h
};

struct gtBmpExtension
{
    unsigned long	current_time;
    unsigned char	device_status;
    unsigned long	azimuth_angle;
    unsigned long	pitch_angle;
    unsigned long	focal_len;
    unsigned long long	distance;
    unsigned long	fraps;
    unsigned long	integration_time;
    unsigned char	reserved[31];
};

struct gtGrey8BmpDesc{
    struct gtBmpHeader header;
    unsigned int *palette;
    unsigned char extension[64];
    unsigned char *data;
};
#pragma pack(pop)
#else
struct gtBmpHeader{
    unsigned short identifiler;
    unsigned int file_size;
    unsigned short reversed1;
    unsigned short reversed2;
    unsigned int bitmap_data_offset;
    unsigned int bitmap_header_size;
    unsigned int width;
    unsigned int height;
    unsigned short planes;
    unsigned short bits_per_pixel;
    unsigned int compression;
    unsigned int bitmap_data_size;
    unsigned int hResolution;
    unsigned int vResolution;
    unsigned int colors;
    unsigned int important_colors;
}__attribute__((__packed__));

struct gtGrey8BmpDesc{
    struct gtBmpHeader header;
    unsigned int *palette;
    unsigned char *data;
}__attribute__((__packed__));
#endif

#define GREY_8BIT_BMP_HEADER_SIZE (sizeof(struct gtBmpHeader) + sizeof(struct gtBmpExtension) + 1024)
#define GREY_16BIT_BMP_HEADER_SIZE (sizeof(struct gtBmpHeader) + sizeof(struct gtBmpExtension))
#define BMP_HEADER_SIZE(type) ((type == BMP_TYPE_8_GREY) ? GREY_8BIT_BMP_HEADER_SIZE : GREY_16BIT_BMP_HEADER_SIZE)
class gtBMP
{
public:
    gtBMP(int _width, int _height, int _color_depth, unsigned char *_buffer, unsigned int _buf_size, bool up_down_reverse, bool left_right_reverse);
    gtBMP(int _width, int _height, unsigned char* buffer, unsigned int _buf_size);

    ~gtBMP();

    void writeToFile(std::wstring file_name);
    unsigned char* get_extension_buffer(){ return bmp.extension; }
    bool isOK() { return ok; }
    gtBmpHeader& getHeader()
    {
        return bmp.header;
    }
private:
    bool ok;
    bool own_data;
    struct gtGrey8BmpDesc bmp;
    //	gtBmpType type;
};

// void setExtraInfo(gtBmpExtension* extension, int _width, int _height, int color_depth,
// 	int data_source, unsigned char *_buffer, unsigned int _buf_size, long vendor, gtBmpHeader* header = NULL);
// void setExtraInfo(gtBmpExtension* extension, unsigned char* buffer, unsigned int buffer_size, 
// 	long vendor, gtBmpHeader* header = NULL);

typedef struct tagDFMCODE
{
    int du;
    int fen;
    int miao;
    int millmiao;
}DFMCODE;